﻿using RestaurantClassLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace Restaurant.Controllers
{
    public class WebServiceController : ApiController
    {
        // GET api/webservice
        // get summary info for next 7 days
        public IEnumerable<DailySummary> GetWeeklySummary()
        {
            RestaurantEntities context = new RestaurantEntities();
            //create a list to hold the 7 daily summaries for the week
            List<DailySummary> weeklySummary = new List<DailySummary>(7);

            //construct a DailySummary object for each of the next 7 days and then add them all to the list
            DailySummary todaySummary = new DailySummary() { Date = DateTime.Today };
            DailySummary tomorrowSummary = new DailySummary() { Date = DateTime.Today.AddDays(1) };
            DailySummary day3Summary = new DailySummary() { Date = DateTime.Today.AddDays(2) };
            DailySummary day4Summary = new DailySummary() { Date = DateTime.Today.AddDays(3) };
            DailySummary day5Summary = new DailySummary() { Date = DateTime.Today.AddDays(4) };
            DailySummary day6Summary = new DailySummary() { Date = DateTime.Today.AddDays(5) };
            DailySummary day7Summary = new DailySummary() { Date = DateTime.Today.AddDays(6) };

            weeklySummary.Add(todaySummary);
            weeklySummary.Add(tomorrowSummary);
            weeklySummary.Add(day3Summary);
            weeklySummary.Add(day4Summary);
            weeklySummary.Add(day5Summary);
            weeklySummary.Add(day6Summary);
            weeklySummary.Add(day7Summary);

            //For each day, check if there are bookings and change the DailySummary properties if there are
            foreach (DailySummary summary in weeklySummary)
            {
                var reservations = context.Reservations.Where(r => r.Date == summary.Date);
                if (reservations.Count() > 0)
                {
                    summary.NumReservations = reservations.Count();
                    summary.NumSeats = reservations.Select(r => r.Seats).Sum();   
                }
            }

            //return the completed list  of daily summaries for the next week
            return weeklySummary;
        }

        // GET api/webservice/date
        // get reservations for a particular date
        public IEnumerable<Reservation> GetReservationsForDate(DateTime date)
        {
            RestaurantEntities context = new RestaurantEntities();

            var reservationsForDate = context.Reservations.Where(r => r.Date == date);

            return reservationsForDate;
        }

        // GET api/webservice/startDate/endDate
        // get reservations for period between startDate and endDate parameters
        //public IEnumerable<Reservation> GetReservationsForPeriod(DateTime startDate, DateTime endDate)
        //{
        //    RestaurantEntities context = new RestaurantEntities();

        //    var reservationsForPeriod = context.Reservations.Where(r => (r.Date >= startDate && r.Date <= endDate)).OrderBy(r => r.Date);

        //    return reservationsForPeriod;
        //}
    }
}
